package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang
 * 高级搜索中车辆的基本详细controller
 */
public class CarSearchMarketDetailController extends TmsController{
	private Logger logger = Logger.getLogger(CarSearchMarketDetailController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String serial = (String)uc.getAttribute("serial");
			Car car = Car.getInstance();
			car = car.findCarBySerial(serial);
			uc.setAttribute("licensePlate", car.getLicensePlate());
			
			CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
			if(car != null){
				setViewhelperFromEntity(carCreationInputViewHelper,car);
			}

			//
			Member member = new Member();
			member = member.getMember(String.valueOf(car.getMemberId()));
			carCreationInputViewHelper.setContractType(member.getContractType());
			
			TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
			transferAcceptedInfo = transferAcceptedInfo.getTransferBybasicCarId(serial);
			carCreationInputViewHelper.setTransferSerial(transferAcceptedInfo.getSerial());

			uc.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
			
			controller.commit();
			
			
			} catch (Exception e) {
				try {
					controller.rollback();
				} catch (TransactionException e1) {
					throw new TmsException(TmsException.DB_TXCRROR, e1);
				}
				// 系统异常
				if (e instanceof TmsException) {
					throw e;
				}
				throw new TmsException(TmsException.DB_TXCRROR, e);
			}
		logger.debug(this.getClass().getName() + "#execute was completed.");
			
	}
	/**
	 * 从Entity 给viewHelper 赋值
	 * 
	 * @param viewHelper
	 * @param car
	 * @throws TmsDaoException
	 */
	private void setViewhelperFromEntity(CarCreationInputViewHelper viewHelper,
			Car car) throws TmsDaoException {
		Codes code = Codes.getInstance();
		viewHelper.setSerial(car.getSerial());//车辆编号
		viewHelper.setTempLicensePlate(car.getTempLicensePlate());
		Member mem = Member.getInstance();
		viewHelper.setMemberId(car.getMemberId()+"");//商户名称
		viewHelper.setMemberName(mem.getMemNameByMemId(car.getMemberId()));
		if(car.getCatClass() == 1){
			viewHelper.setCarType("商用车");//车辆种类
		}else{
			viewHelper.setCarType("乘用车");//车辆种类
		}		
		viewHelper.setLicensePlate(car.getLicensePlate());//车牌号码
		viewHelper.setVin(car.getVin());//车架号
		viewHelper.setIsShelveInfo(code.getItemValueByItemIdAndItemCode(Codes.ISSHELVE_TYPE, car.getIsShelve()));//是否上架
		viewHelper.setIsMatchInfo(code.getItemValueByItemIdAndItemCode(Codes.ISMATCH_TYPE, car.getIsMatch()));//车辆是否匹对
		viewHelper.setCarName(car.getCarName());//车辆名称
		if (car.getMileage() == 0) {//表显里程
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(StringUtil.addDH(Integer.toString(car.getMileage())));
		}
		if (car.getPrice() == 0) {//价格
			viewHelper.setPrice("");
		} else {
			viewHelper.setPrice(StringUtil.addDH(Integer.toString(car.getPrice())));
		}
		viewHelper.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,car.getColor()));//车身颜色
		viewHelper.setRegDate(car.getRegDate());//登录日期
		viewHelper.setJqx(car.getJqx());//交强险日期
		viewHelper.setYearCarefulPeriod(car.getYearCarefulPeriod());//年检日期
		viewHelper.setCompanyContactPerson(car.getCompanyContactPerson());//联系人
		viewHelper.setCompanyContactPhone(car.getCompanyContactPhone());//联系电话
		viewHelper.setRemark(car.getRemark());//配置说明
		//图片信息
		viewHelper.setImg1(car.getPhoto_1());
		viewHelper.setImg2(car.getPhoto_2());
		viewHelper.setImg3(car.getPhoto_3());
		viewHelper.setImg4(car.getPhoto_4());
		viewHelper.setImg5(car.getPhoto_5());
		viewHelper.setImg6(car.getPhoto_6());
		viewHelper.setImg7(car.getPhoto_7());
		viewHelper.setImg8(car.getPhoto_8());
		viewHelper.setStatus(car.getStatus());
		
		viewHelper.setRegisterStatus(car.getRegisterStatus());


	}
}
